﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using Hkwl.WorldExpo.Merchant.Logs;
using System.Data.OleDb;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ClientAdd : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            NxClient client = new NxClient();
            if (txtCellPhone.Text.Trim() != "" && txtCompany.Text.Trim() != "" && txtUserName.Text.Trim() != "")
            {
                client.Cellphone = txtCellPhone.Text.Trim();
                client.UserName = txtUserName.Text.Trim();
                client.Company = txtCompany.Text.Trim();
                client.Sex = int.Parse(rblSex.SelectedValue.ToString());
                client.Age = int.Parse(txtAge.Text.Trim());
                client.Status = 1;
                int merchantId = 1;
                if (Session["MerchantId"] != null)
                {
                    merchantId = int.Parse(Session["MerchantId"].ToString());
                    client.MerchantId = merchantId;
                }
                if (!ServiceLocator.IClientService.CheckNxClientByCellphone(client.Cellphone, merchantId))
                {
                    if (!ServiceLocator.IClientService.InsertNxClient(client))
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_ERROR") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                    }
                    else
                    {
                        //添加日志
                        ServiceLocator.IMerchantService.CommonInsertFormatLog(Convert.ToInt32(Session["MerchantId"]), 1, string.Format("新增客户操作，新增客户名称：{0}", this.txtUserName.Text));
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_SUCCESSFUL") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','ClientCreate.aspx')", this.Page);
                    }
                }
                else
                {
                    JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "ALREADY_EXISTS") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                    Log4Net.InfoLog().Info(client.Cellphone);
                }
            }
            else
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "NUMBER_IS_REQUIRED") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
            }
        }

        protected void btnSubmit2_Click(object sender, EventArgs e)
        {
            if (this.FileUpload_Excel.HasFile)
            {
                int merchantId = 1;
                if (Session["MerchantId"] != null)
                {
                    merchantId = int.Parse(Session["MerchantId"].ToString());
                }
                try
                {
                    //获取文件名
                    string FileName = this.FileUpload_Excel.FileName.ToString();
                    //获取文件后缀名
                    string[] FileArray = FileName.Split('.');
                    string FileType = FileArray[FileArray.Length - 1].ToString();

                    //判断上传的文件是否为Excel格式
                    if (FileType != "xls")
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "UPLOAD_EXCEL") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                        return;
                    }

                    this.FileUpload_Excel.SaveAs(Server.MapPath("~") + "/Temp/" + "TempExcel" + "." + FileType);

                    //先将EXCEL导入到数据库,一:先把EXCEL导入dateView,二:然后将dateView里的数据导入到数据库里面

                    //EXCEL连接
                    string conn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:/www/Merchant/Temp/TempExcel.xls;" + "Extended Properties=Excel 8.0;";
                    OleDbConnection thisconnection = new OleDbConnection(conn);
                    thisconnection.Open();
                    string Sql = "select * from [Sheet1$]";
                    OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
                    DataSet ds = new DataSet();
                    mycommand.Fill(ds, "[Sheet1]");
                    thisconnection.Close();

                    NxClient client = new NxClient();
                    int count = ds.Tables["[Sheet1]"].Rows.Count;

                    for (int i = 0; i < count; i++)
                    {
                        if (!ServiceLocator.IClientService.CheckNxClientByCellphone(ds.Tables["[Sheet1]"].Rows[i]["手机号码"].ToString().Trim(), merchantId))
                        {
                            client.Cellphone = ds.Tables["[Sheet1]"].Rows[i]["手机号码"].ToString();
                            client.Company = ds.Tables["[Sheet1]"].Rows[i]["公司名称"].ToString();
                            client.UserName = ds.Tables["[Sheet1]"].Rows[i]["客户姓名"].ToString();
                            if (ds.Tables["[Sheet1]"].Rows[i]["性别"].ToString() == "男" || ds.Tables["[Sheet1]"].Rows[i]["性别"].ToString() == "Male")
                            {
                                client.Sex = 1;
                            }
                            else
                            {
                                client.Sex = 2;
                            }
                            client.Age = int.Parse(ds.Tables["[Sheet1]"].Rows[i]["年龄"].ToString());
                            client.MerchantId = merchantId;
                            client.Status = 1;
                            if (!ServiceLocator.IClientService.InsertNxClient(client))
                            {
                                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_ERROR") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                            }
                        }
                        if (i + 1 == count)
                        {
                            JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_SUCCESSFUL") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','ClientCreate.aspx')", this.Page);
                        }
                    }
                }
                catch (Exception ex)
                {
                    JavaScript.WriteJs("jAlert('" + ex.Message.ToString()+ "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                }
            }
            else
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "UPLOAD_ERR") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
            }
        }
    }
}
